package interviewClassic150;

import org.junit.Test;

public class IsValidSudoku {
    public boolean isValidSudoku(char[][] board) {
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                //依次遍历矩阵中的元素
                //空格则跳过
                if (board[i][j] == '.') continue;
                //非空格，数字，则判断同一行，同一列是否有相等的元素
                for (int k = 0; k < 9; k++) {
                    char sameRowChar = board[i][k];
                    char sameColumnChar = board[k][j];
                    int containerStartRow = (i / 3) * 3;
                    int containerStartColumn = (j / 3) * 3;
                    int containerRow = k / 3;
                    int containerColumn = k % 3;
                    int RowInd = containerStartRow + containerRow;//遍历容器中的行索引
                    int ColInd = containerStartColumn + containerColumn;//遍历容器中的列索引
                    char containerChar = board[RowInd][ColInd];
                    if (k == j) sameRowChar = '.';
                    if (k == i) sameColumnChar = '.';
                    if (RowInd == i && ColInd == j) containerChar = '.';
                    if (sameRowChar == board[i][j]
                            || sameColumnChar == board[i][j]
                            || containerChar == board[i][j]) return false;
                }
            }
        }

        return true;
    }

    @Test
    public void test() {

    }
}
